home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CGAMEJOY.ARJ / JOYSTKMD.C < prev    next >
C/C++ Source or Header  |  1992-01-08  |  17KB  |  399 lines

  1. /* joystkmd.c       Middle level joystick functions built on joystklo.c.
  2.  
  3.                 The functions in this module
  4.                     1.  detect and initialize a joystick coordinate system.
  5.                     2.  read changed joystick position
  6.                     3.  provide a rough translation from the top-left origin
  7.                         joystick coordinate system to a balanced 0,0
  8.                         centered cartesian system
  9.                     4.  get/reset joystick center coords
  10.                     5.  get/reset joystick minimum coords
  11.                     6.  get/reset joystick maximum coords
  12.                     7.  get/set an ignore threshold (tolerance) for
  13.                         reading changed joystick positions
  14.  
  15.                 See file joystkmd.se for pseudo code/structured english
  16.                 and joystick.doc for an explanation of the functions.
  17.  
  18.                 This module was written for Borland C++ 2.0.
  19.                 No C++ extensions were used, no Assembler.
  20.                 This is all native C.
  21.  
  22.                 I've done limited testing on an old XT with 11/08/82 BIOS
  23.                 and on my PS/2 like 386 AT and haven't found anything yet...
  24.  
  25.                 One thing... the initialization function is intended for
  26.                 a single user joystick installation.  Multiple joysticks
  27.                 may be initialized seperately by using the Get/SetCenter,
  28.                 Get/SetMax, and Get/SetMin functions in a fashion similar
  29.                 to my initialization algorithm.
  30.  
  31.                 If you find this module useful, great!!!
  32.  
  33.                 This module has been released to the public domain
  34.                         Tue  01-07-1992   23:30:18
  35.                 by Ed Torgerson : CIS 70313,456,  GEnie E.TORGERSON1.
  36.                 any constructive correspondence is welcome.
  37.  
  38. -----------------------------------------------------------------------------*/
  39.  
  40.  
  41. #include <stdio.h>
  42. #include <conio.h>
  43. #include "joystick.h"
  44.  
  45. /*-- Global Typedefs --------*/                
  46. struct StickValues {
  47.         int         jax ;       /* joystick A x-coordinate value */
  48.         int         jay ;       /* joystick A y-coordinate value */
  49.         int         jbx ;       /* joystick B x-coordinate value */
  50.         int         jby ;       /* joystick B y-coordinate value */
  51. } ;
  52.  
  53. struct Tolerance {
  54.         int         axl ;     /* joystick A x-low breakpoint */
  55.         int         axm ;     /* joystick A x-mid breakpoint */
  56.         int         axh ;     /* joystick A x-hi  breakpoint */
  57.         int         ayl ;     /* joystick A y-low breakpoint */
  58.         int         aym ;     /* joystick A y-mid breakpoint */
  59.         int         ayh ;     /* joystick A y-hi  breakpoint */
  60.         int         bxl ;     /* joystick B x-low breakpoint */
  61.         int         bxm ;     /* joystick B x-mid breakpoint */
  62.         int         bxh ;     /* joystick B x-hi  breakpoint */
  63.         int         byl ;     /* joystick B y-low breakpoint */
  64.         int         bym ;     /* joystick B y-mid breakpoint */
  65.         int         byh ;     /* joystick B y-hi  breakpoint */
  66.         char        t1  ;     /* low graded tolerance */
  67.         char        t2  ;     /* mid graded tolerance */
  68.         char        t3  ;     /* hi  graded tolerance */
  69. } ;
  70.  
  71.  
  72. /*-- Global variables -----*/
  73. struct StickValues  G_Stick_Centers ;   /* coords of joystick centers */
  74. struct StickValues  G_Stick_Minimums ;  /* minimum joystick coordinates */
  75. struct StickValues  G_Stick_Maximums ;  /* maximum joystick coordinates */
  76. struct StickValues  G_Stick_Last_Pos ;  /* joystick coordinates last read */
  77. int                 G_Stick_Tolerance ; /* maximum scaled ignore threshold */
  78. struct Tolerance    G_Stick_Breaks ;    /* breakpoints for tolerance scaling */
  79.  
  80.  
  81. /*-- Function Prototypes of Internal Subroutines -----*/
  82. int jst_init_sub  ( int msg, int stop_button, struct StickValues *what,
  83.                     void (*NotifyUserFunc)(int)  ) ;
  84. int jst_set_coord_sub ( int stick, int function ) ;
  85.  
  86.  
  87. /*-- Function Definitions ---------------------------------------------------*/
  88. int JstInitialize ( void (*NotifyUserFunc)(int) )
  89. {
  90.     register int    retval, Jstk ;
  91.  
  92.     JstSetActiveAxes( 0 ) ;
  93.     retval = JstDetect () ;
  94.     Jstk = (JAX_AXIS | JAY_AXIS) ;      /* test for presence of Joystick A */
  95.     if ( (retval & Jstk) == (Jstk) )
  96.     {
  97.         JstSetActiveAxes( Jstk ) ;       /* set joystick axes mask active */
  98.     }
  99.     Jstk = (JBX_AXIS | JBY_AXIS) ;      /* test for presence of Joystick B */
  100.     if ( (retval & Jstk) == (Jstk) )
  101.     {
  102.         JstSetActiveAxes( Jstk | JstGetActiveAxes() ) ;   /* add jstk mask */
  103.     }
  104.     if ( JstGetActiveAxes() == 0 )     /* if a 2-axis joystick is not found */
  105.     {
  106.         NotifyUserFunc(0) ;             /* notify user */
  107.         return (0) ;                    /* return error code */
  108.     }
  109.     NotifyUserFunc ( 1 ) ;              /* notify user: press escape to quit */
  110.     if ( jst_init_sub( 2, J_ANY_BUTTON_DOWN, &G_Stick_Centers, NotifyUserFunc )
  111.             == 0 )
  112.         return (0) ;                    /* return error code */
  113.                                         /* get center coordinates from user */
  114.     if ( jst_init_sub( 3, J_BUTTON_1_DOWN, &G_Stick_Minimums, NotifyUserFunc )
  115.             == 0 )
  116.         return (0) ;                    /* return error code */
  117.                                         /* get minimum coordinates from user */
  118.     if ( jst_init_sub (  4, J_BUTTON_2_DOWN,&G_Stick_Maximums, NotifyUserFunc )
  119.             == 0 )
  120.         return (0) ;                    /* return error code */
  121.                                         /* get maximum coordinates from user */
  122.     G_Stick_Last_Pos.jax = 0 ;          /* zero out last position */
  123.     G_Stick_Last_Pos.jay = 0 ;
  124.     G_Stick_Last_Pos.jbx = 0 ;
  125.     G_Stick_Last_Pos.jby = 0 ;                                      
  126.     if ( G_Stick_Maximums.jax != 0 )        /* set tolerance for events */
  127.     {
  128.         Jstk = G_Stick_Maximums.jax + G_Stick_Maximums.jax ;
  129.         G_Stick_Tolerance = Jstk >> 6 ;
  130.     }
  131.     else if ( G_Stick_Maximums.jbx != 0 )
  132.     {
  133.         Jstk = G_Stick_Maximums.jbx + G_Stick_Maximums.jbx ;
  134.         G_Stick_Tolerance = Jstk >> 6 ;
  135.     }
  136.     JstSetTolerance ( G_Stick_Tolerance ) ;
  137.     return ( 1 ) ;                                          /* success */
  138. }  /* end JstInitialize ---*/
  139.  
  140.             /*------------------------------------------*/
  141. int jst_init_sub ( int msg, int stop_button, struct StickValues *what,
  142.                     void (*NotifyUserFunc)(int) )
  143.                                     /* subroutine to get input from user */
  144. {
  145.     register int    retval ;
  146.  
  147.     NotifyUserFunc( msg ) ;
  148.     retval = 0 ;
  149.     do
  150.     {
  151.         if ( kbhit() )
  152.         {
  153.             retval = getch() ;
  154.             if (retval == 27)                   /* user pressed escape */
  155.             {
  156.                 NotifyUserFunc (-1) ;           /* user terminate */
  157.                 NotifyUserFunc (-2) ;           /* "incomplete" warning */
  158.                 return(0) ;
  159.             }
  160.         }
  161.         retval = JstGetChangedButton() ;
  162.     } while ( (retval & stop_button) == 0 ) ;
  163.     what->jax = JstGetPosition( JAX_AXIS  ) ;
  164.     what->jay = JstGetPosition( JAY_AXIS  ) ;
  165.     what->jbx = JstGetPosition( JBX_AXIS  ) ;
  166.     what->jby = JstGetPosition( JBY_AXIS  ) ;
  167.     return (1) ;
  168. }  /* end jst_init_sub ---*/
  169.  
  170. /*---------------------------------------------------------------------*/
  171. int JstGetChangedPosition ( int stick, struct JoystickPosition *stickpos )
  172.                                            /* returns changes in active axes */
  173. {
  174.     int    x_pos, y_pos, xtol, ytol ;
  175.  
  176.     if ( (stick == JOYSTICK_A) || (stick == JOYSTICK_B) )
  177.     {
  178.         if ( (stick & JstGetActiveAxes() ) == stick )
  179.         {
  180.             xtol = ytol = G_Stick_Tolerance ;
  181.             if (stick == JOYSTICK_A)
  182.             {
  183.                 x_pos = JstGetPosition( JAX_AXIS ) ;
  184.                     if ( x_pos < G_Stick_Breaks.axl )
  185.                         xtol = G_Stick_Breaks.t1 ;
  186.                     else if ( x_pos < G_Stick_Breaks.axm )
  187.                         xtol = G_Stick_Breaks.t2 ;
  188.                     else if ( x_pos < G_Stick_Breaks.axh )
  189.                         xtol = G_Stick_Breaks.t3 ;
  190.                 y_pos = JstGetPosition( JAY_AXIS ) ;
  191.                     if ( y_pos < G_Stick_Breaks.ayl )
  192.                         ytol = G_Stick_Breaks.t1 ;
  193.                     else if ( y_pos < G_Stick_Breaks.aym )
  194.                         ytol = G_Stick_Breaks.t2 ;
  195.                     else if ( y_pos < G_Stick_Breaks.ayh )
  196.                         ytol = G_Stick_Breaks.t3 ;
  197.                 if ( (x_pos < G_Stick_Last_Pos.jax - xtol ) ||
  198.                      (x_pos > G_Stick_Last_Pos.jax + xtol ) ||
  199.                      (y_pos < G_Stick_Last_Pos.jay - ytol ) ||
  200.                      (y_pos > G_Stick_Last_Pos.jay + ytol ) )
  201.                 {
  202.                     G_Stick_Last_Pos.jax = x_pos ;
  203.                     G_Stick_Last_Pos.jay = y_pos ;
  204.                     stickpos->jx = x_pos ;
  205.                     stickpos->jy = y_pos ;
  206.                     return ( 1 ) ;                  /* change */
  207.                 }
  208.             }
  209.             else if (stick == JOYSTICK_B)
  210.             {
  211.                 x_pos = JstGetPosition( JBX_AXIS ) ;
  212.                     if ( x_pos < G_Stick_Breaks.bxl )
  213.                         xtol = G_Stick_Breaks.t1 ;
  214.                     else if ( x_pos < G_Stick_Breaks.bxm )
  215.                         xtol = G_Stick_Breaks.t2 ;
  216.                     else if ( x_pos < G_Stick_Breaks.bxh )
  217.                         xtol = G_Stick_Breaks.t3 ;
  218.                 y_pos = JstGetPosition( JBY_AXIS ) ;
  219.                     if ( y_pos < G_Stick_Breaks.byl )
  220.                         ytol = G_Stick_Breaks.t1 ;
  221.                     else if ( y_pos < G_Stick_Breaks.bym )
  222.                         ytol = G_Stick_Breaks.t2 ;
  223.                     else if ( y_pos < G_Stick_Breaks.byh )
  224.                         ytol = G_Stick_Breaks.t3 ;
  225.                 if ( (x_pos < G_Stick_Last_Pos.jbx - xtol ) ||
  226.                      (x_pos > G_Stick_Last_Pos.jbx + xtol ) ||
  227.                      (y_pos < G_Stick_Last_Pos.jby - ytol ) ||
  228.                      (y_pos > G_Stick_Last_Pos.jby + ytol ) )
  229.                 {
  230.                     G_Stick_Last_Pos.jbx = x_pos ;
  231.                     G_Stick_Last_Pos.jby = y_pos ;
  232.                     stickpos->jx = x_pos ;
  233.                     stickpos->jy = y_pos ;
  234.                     return ( 1 ) ;                  /* change */
  235.                 }
  236.             }
  237.         }
  238.     }
  239.     return ( 0 ) ;
  240. }   /*-- end JstGetChangedPosition ----*/
  241. /*---------------------------------------------------------------------*/
  242. void JstXlatToCart ( int stick, struct JoystickPosition *stickpos )
  243.                 /* translates axis position relative to the center in
  244.                     rough cartesian coordinates */
  245. {
  246.     if (stick == JOYSTICK_A)
  247.     {
  248.         stickpos->jx = stickpos->jx - G_Stick_Centers.jax ;
  249.         if ( stickpos->jx > 0 )
  250.             stickpos->jx = stickpos->jx >> 1 ;
  251.         stickpos->jy = -(stickpos->jy - G_Stick_Centers.jay) ;
  252.         if ( stickpos->jy < 0 )
  253.             stickpos->jy = stickpos->jy / 2 ;
  254.     }
  255.     else if (stick == JOYSTICK_B)
  256.     {
  257.         stickpos->jx = stickpos->jx - G_Stick_Centers.jbx ;
  258.         if ( stickpos->jx > 0 )
  259.             stickpos->jx = stickpos->jx >> 1 ;
  260.         stickpos->jy = -(stickpos->jy - G_Stick_Centers.jby) ;
  261.         if ( stickpos->jy < 0 )
  262.             stickpos->jy = stickpos->jy / 2 ;
  263.     }
  264.     return ;
  265. }
  266. /*---------------------------------------------------------------------*/
  267. void JstGetCenter( int stick, struct JoystickPosition *jpos )
  268.                             /* gets the set center values for the joystick */
  269. {
  270.     if (stick == JOYSTICK_A)
  271.     {
  272.         jpos->jx = G_Stick_Centers.jax ;
  273.         jpos->jy = G_Stick_Centers.jay ;
  274.     }
  275.     if (stick == JOYSTICK_B)
  276.     {
  277.         jpos->jx = G_Stick_Centers.jbx ;
  278.         jpos->jy = G_Stick_Centers.jby ;
  279.     }
  280. }
  281. /*---------------------------------------------------------------------*/
  282. void JstGetMin( int stick, struct JoystickPosition *jpos )
  283.                             /* gets the set minimum values for the joystick */
  284. {
  285.     if (stick == JOYSTICK_A)
  286.     {
  287.         jpos->jx = G_Stick_Minimums.jax ;
  288.         jpos->jy = G_Stick_Minimums.jay ;
  289.     }
  290.     if (stick == JOYSTICK_B)
  291.     {
  292.         jpos->jx = G_Stick_Minimums.jbx ;
  293.         jpos->jy = G_Stick_Minimums.jby ;
  294.     }
  295. }
  296. /*---------------------------------------------------------------------*/
  297. void JstGetMax( int stick, struct JoystickPosition *jpos)
  298.                             /* gets the set maximum values for the joystick */
  299. {
  300.     if (stick == JOYSTICK_A)
  301.     {
  302.         jpos->jx = G_Stick_Maximums.jax ;
  303.         jpos->jy = G_Stick_Maximums.jay ;
  304.     }
  305.     if (stick == JOYSTICK_B)
  306.     {
  307.         jpos->jx = G_Stick_Maximums.jbx ;
  308.         jpos->jy = G_Stick_Maximums.jby ;
  309.     }
  310. }
  311. /*---------------------------------------------------------------------*/
  312. int JstSetCenter( int stick )   /* grabs current pos as new joystick center */
  313. {
  314.     return (jst_set_coord_sub( stick, 1 )) ;
  315. }
  316. /*---------------------------------------------------------------------*/
  317. int JstSetMin( int stick )      /* grabs current pos as new minimum */
  318. {
  319.     return (jst_set_coord_sub( stick, 2 )) ;
  320. }
  321. /*---------------------------------------------------------------------*/
  322. int JstSetMax( int stick )      /* grabs current pos as new maximum */
  323. {
  324.     return (jst_set_coord_sub( stick, 3 )) ;
  325. }
  326. /*---------------------------------------------------------------------*/
  327. int jst_set_coord_sub ( int stick, int function )
  328. {
  329.     struct StickValues *ptrStickValues ;
  330.  
  331.     if ( (stick == JOYSTICK_A) || (stick == JOYSTICK_B) )
  332.     {
  333.         if ( (stick & JstGetActiveAxes()) == stick )
  334.         {
  335.             if ( function == 1 )
  336.                 ptrStickValues = &G_Stick_Centers ;
  337.             if ( function == 2 )
  338.                 ptrStickValues = &G_Stick_Minimums ;
  339.             if ( function == 3 )
  340.                 ptrStickValues = &G_Stick_Maximums ;
  341.             if ( stick == JOYSTICK_A )
  342.             {
  343.                 ptrStickValues->jax = JstGetPosition( JAX_AXIS  ) ;
  344.                 ptrStickValues->jay = JstGetPosition( JAY_AXIS  ) ;
  345.             }
  346.             if ( stick == JOYSTICK_B )
  347.             {
  348.                 ptrStickValues->jbx = JstGetPosition( JBX_AXIS ) ;
  349.                 ptrStickValues->jby = JstGetPosition( JBY_AXIS ) ;
  350.             }
  351.             JstSetTolerance ( G_Stick_Tolerance ) ;
  352.             return ( 1 ) ;
  353.         }
  354.     }
  355.     return ( 0 ) ;
  356. }  /* end jst_set_coord_sub ---*/
  357. /*---------------------------------------------------------------------*/
  358. int JstGetTolerance ()                  /* get tolerance of change to ignore */
  359. {
  360.     return ( G_Stick_Tolerance ) ;
  361. }
  362. /*---------------------------------------------------------------------*/
  363. void JstSetTolerance (int tolerance)    /* set tolerance of change to ignore */
  364. {
  365.     G_Stick_Tolerance = tolerance ;
  366.     if ( tolerance > 7 )
  367.     {
  368.         G_Stick_Breaks.axl = G_Stick_Centers.jax >> 2 ;
  369.         G_Stick_Breaks.axm = G_Stick_Centers.jax >> 1 ;
  370.         G_Stick_Breaks.axh = G_Stick_Maximums.jax >> 1 ;
  371.         G_Stick_Breaks.ayl = G_Stick_Centers.jay >> 2 ;
  372.         G_Stick_Breaks.aym = G_Stick_Centers.jay >> 1 ;
  373.         G_Stick_Breaks.ayh = G_Stick_Maximums.jay >> 1 ;
  374.         G_Stick_Breaks.bxl = G_Stick_Centers.jbx >> 2 ;
  375.         G_Stick_Breaks.bxm = G_Stick_Centers.jbx >> 1 ;
  376.         G_Stick_Breaks.bxh = G_Stick_Maximums.jbx >> 1 ;
  377.         G_Stick_Breaks.byl = G_Stick_Centers.jby >> 2 ;
  378.         G_Stick_Breaks.bym = G_Stick_Centers.jby >> 1 ;
  379.         G_Stick_Breaks.byh = G_Stick_Maximums.jby >> 1 ;
  380.         G_Stick_Breaks.t1 = (tolerance >> 2) - 1 ;
  381.         G_Stick_Breaks.t2 = tolerance >> 2 ;
  382.         G_Stick_Breaks.t3 = tolerance >> 1 ;
  383.     }
  384.     else
  385.     {
  386.         G_Stick_Breaks.axl = G_Stick_Breaks.axm = G_Stick_Breaks.axh = 0 ;
  387.         G_Stick_Breaks.ayl = G_Stick_Breaks.aym = G_Stick_Breaks.ayh = 0 ;
  388.         G_Stick_Breaks.bxl = G_Stick_Breaks.bxm = G_Stick_Breaks.bxh = 0 ;
  389.         G_Stick_Breaks.byl = G_Stick_Breaks.bym = G_Stick_Breaks.byh = 0 ;
  390.         G_Stick_Breaks.t1 = tolerance ;
  391.         G_Stick_Breaks.t2 = tolerance ;
  392.         G_Stick_Breaks.t3 = tolerance ;
  393.     }
  394.     return ;
  395. }
  396. /*---------------------------------------------------------------------*/
  397.                     
  398. /* end joystkmd.c  */
  399.